package com.idbk.chargestation.activity.charge;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.APIForCharge;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonBase;
import com.idbk.chargestation.bean.JsonPointPile;
import com.idbk.chargestation.bean.JsonQrScan;
import com.idbk.chargestation.dialog.DialogResult;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GsonUtils;

public class ActivityLock extends BaseActivity implements OnClickListener{

	private TextView mPoint, mSn, mStyle, mCost, mPerformance;
	private JsonQrScan mQrScanData;
	private TextView mLockA, mLockB, mNext;
	private ImageView mCheckA, mCheckB;
	private RelativeLayout mRelateGunA, mRelateGunB;
	private String mStrLock;
	private boolean isAEmpty = false, isBEmpty = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_lock);

		setupView();

		setupData();
	}
	
	private void setupView() {
		setupToolBar2();
				
		mPoint = (TextView) findViewById(R.id.point);
		mSn = (TextView) findViewById(R.id.sn);
		mStyle = (TextView) findViewById(R.id.style);
		mCost = (TextView) findViewById(R.id.cost);
		mPerformance = (TextView) findViewById(R.id.performance);
		mLockA = (TextView) findViewById(R.id.gunA);
		mLockB = (TextView) findViewById(R.id.gunB);
		mCheckA = (ImageView) findViewById(R.id.checkA);
		mCheckB = (ImageView) findViewById(R.id.checkB);
		
		mRelateGunA = (RelativeLayout) findViewById(R.id.layout_gunA);
		mRelateGunB = (RelativeLayout) findViewById(R.id.layout_gunB);
		mNext = (TextView) findViewById(R.id.textview_next);
		if (mNext != null) {
			mNext.setOnClickListener(this);
		}

	}
	
	private void setupData() {		
		mQrScanData = getIntent().getParcelableExtra(JsonQrScan.KEY);
		if (mQrScanData == null) {
			Toast.makeText(this, "数据传输错误", Toast.LENGTH_SHORT).show();
		}
		mPoint.setText(mQrScanData.pointName);
		mSn.setText(mQrScanData.pileSn);
		mStyle.setText(mQrScanData.performance);
		mCost.setText(mQrScanData.getPriceTypeString());
		mPerformance.setText(mQrScanData.chargeSpeed);
		pileChoose(mQrScanData);
	}
	
	private void pileChoose(JsonQrScan mQrScanData) {
		if (mQrScanData.pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_ASYN 
				|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_SYN
				|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_SINGLE_PHASE
				|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_THREE_PHASE
				) {
			//地锁1和地锁2的状态显示
			showLockAStatue(mQrScanData.lock1);
			showLockBStatue(mQrScanData.lock2);
			
		} else if(mQrScanData.pileType == JsonPointPile.PILE_TYPE_DC
				|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_AC_SINGLE_PHASE
				|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_AC_THREE_PHASE
				){
			if (mQrScanData.lock1 == JsonPointPile.STATUS_LOCK) {
				mLockA.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
						ContextCompat.getDrawable(ActivityLock.this, R.drawable.ico_charging_packing_lock_up));
				mRelateGunA.setOnClickListener(this);
				mRelateGunB.setVisibility(View.GONE);
				
			} else {
				mLockB.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
						ContextCompat.getDrawable(ActivityLock.this, R.drawable.ico_charging_packing_lock_up));
				mRelateGunB.setOnClickListener(this);
				mRelateGunA.setVisibility(View.GONE);
			}
		} else {
			showLockAStatue(mQrScanData.lock1);
			showLockBStatue(mQrScanData.lock2);
		}
	}
	
	private void showLockAStatue(int statueNum) {
		if (statueNum == JsonPointPile.STATUS_LOCK) {
			mLockA.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
					ContextCompat.getDrawable(ActivityLock.this, R.drawable.ico_charging_packing_lock_up));
			mRelateGunA.setOnClickListener(this);
			
		} else if (statueNum == JsonPointPile.STATUS_NONE) {
			mLockA.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
					ContextCompat.getDrawable(ActivityLock.this, R.drawable.ico_charging_packing_lock_none));
			isAEmpty = true;
			
		} else {
			mLockA.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
					ContextCompat.getDrawable(ActivityLock.this, R.drawable.ico_charging_packing_lock_disabled));
			mCheckA.setVisibility(View.GONE);
		}
	}
	
	private void showLockBStatue(int statueNum) {
		if (statueNum == JsonPointPile.STATUS_LOCK) {
			mLockB.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
					ContextCompat.getDrawable(ActivityLock.this, R.drawable.ico_charging_packing_lock_up));
			mRelateGunB.setOnClickListener(this);
			
		} else if (statueNum == JsonPointPile.STATUS_NONE) {
			mLockB.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
					ContextCompat.getDrawable(ActivityLock.this, R.drawable.ico_charging_packing_lock_none));
			mRelateGunB.setOnClickListener(this);
			isBEmpty = true;
			
		} else {
			mLockB.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
					ContextCompat.getDrawable(ActivityLock.this, R.drawable.ico_charging_packing_lock_disabled));
			mCheckB.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.layout_gunA:
			mCheckA.setImageDrawable(ContextCompat.getDrawable(ActivityLock.this, R.drawable.btn_checkbox_active));
			mCheckB.setImageDrawable(ContextCompat.getDrawable(ActivityLock.this, R.drawable.btn_checkbox_normal));
			mNext.setBackgroundResource( R.drawable.bg_main_button_lg_sprite_2);
			if (isAEmpty) {
				mStrLock = "empty";
			} else {
				mStrLock = "01";
			}
 			
			break;
		case R.id.layout_gunB:
			mCheckB.setImageDrawable(ContextCompat.getDrawable(ActivityLock.this, R.drawable.btn_checkbox_active));
			mCheckA.setImageDrawable(ContextCompat.getDrawable(ActivityLock.this, R.drawable.btn_checkbox_normal));		
			mNext.setBackgroundResource( R.drawable.bg_main_button_lg_sprite_2);
			if (isBEmpty) {
				mStrLock = "empty";
			} else {
				mStrLock = "02";
			}
			break;
		case R.id.textview_next:
			submitNext();
			break;

		default:
			break;
		}
		
	}
	
	public void submitNext() {
		System.out.println("mStrLock---->" + mStrLock);
		if (mStrLock == null) {
			Toast.makeText(ActivityLock.this, "请选择要解开的地锁", Toast.LENGTH_SHORT).show();
		} else if (mStrLock.equals("empty")) {
			jumpToGunChoose();
			
		} else {
			showMyProgressDialog(true, false, "正在请求中......");
			mMainRequest = APIForCharge.lockRequest(mQrScanData.pileSn, mStrLock, new EHttpResponse() {
				@Override
				public void onEResponse(String response) {
					// TODO Auto-generated method stub
					JsonBase result = GsonUtils.toBean(JsonBase.class, response);
					if (handleResponseStatus(result)) {
						jumpToGunChoose();
					}
				}
				
				@Override
				public void onEErrorResponse(VolleyError error) {
					Toast.makeText(ActivityLock.this, R.string.common_tip_resultfail, Toast.LENGTH_SHORT).show();
				}

				@Override
				public void onEFinish() {
					// TODO Auto-generated method stub
					dismissMyProgressDialog();
					super.onEFinish();
				}	
			});
		}
	}

	public void jumpToGunChoose() {
			if(mQrScanData.pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_ASYN 
					|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_SYN
					|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_SINGLE_PHASE
					|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_THREE_PHASE) {
				//双枪充电桩
				Intent intent = new Intent(ActivityLock.this, ActivityDoubleGun.class);
				intent.putExtra(JsonQrScan.KEY, mQrScanData);
				startActivity(intent);
				finish();
				
			} else if(mQrScanData.pileType == JsonPointPile.PILE_TYPE_DC
					|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_AC_SINGLE_PHASE
					|| mQrScanData.pileType == JsonPointPile.PILE_TYPE_AC_THREE_PHASE
					){
				//单枪充电桩
				Intent intent = new Intent(ActivityLock.this, ActivitySingleGun.class);
				intent.putExtra(JsonQrScan.KEY, mQrScanData);
				startActivity(intent);
				finish();
				
			} else {
				final DialogResult gunDialog = new DialogResult(ActivityLock.this, false, "电桩不可用", "错误:未知的充电桩类型！");
				gunDialog.show();
			}
		}
}
